/*
 * @Author: your name
 * @Date: 2022-02-09 22:35:33
 * @LastEditTime: 2022-03-23 19:38:59
 * @LastEditors: your name
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \test\bencn_tcp_client.cpp
 */
#include <iostream>
#include "tcp_client.h"
#include "winsock2.h"
#include <stdio.h>
#include <thread>
#include <stdlib.h>

class TcpClientObserverImpl :
	public TcpClientLibevent::TcpClientObserver{
public:
	~TcpClientObserverImpl(){return;}
	mutex mMux;
	virtual void OnConnected() { 
			std::cout<<"client connected"<<std::endl;
	};
	void OnDisConnected() { 
			std::cout<<"client disconnected"<<std::endl;
	};
	void OnData(uint8_t *dat,uint64_t len) {
			std::cout<<"client receive data : ";
			for(int i = 0;i < len;i++){
					std:cout<<dat[i];
			}
	};
	virtual void OnClose(){
			std::cout<<"client close"<<std::endl;;
	}
};
		
using namespace std;
int main() {
	WORD wdVersion = MAKEWORD(2, 2);   //定义自己需要的网络库版本，这里是2.2
	WSADATA wdSockMsg;//这是一个结构体
	int nRes = WSAStartup(wdVersion, &wdSockMsg);//打开一个套接字
	if (0 != nRes) 
	{
		switch (nRes) 
		{
			case WSASYSNOTREADY: 
				printf("check your library");
				break;
			case WSAVERNOTSUPPORTED:
				printf("need updated");
				break;
			case WSAEINPROGRESS: 
				printf("need reboot");
				break;
			case WSAEPROCLIM: 
				break;
		}
	}
	if (2 != HIBYTE(wdSockMsg.wVersion) || 2 != LOBYTE(wdSockMsg.wVersion)) {
		printf("WSACleanup");
		WSACleanup();
		return 0;
	}
	std::cout<<"start client libevent\r\n";
	TcpClientLibevent clint("127.0.0.1",8051,new TcpClientObserverImpl);
	clint.ConnectServer();
	while(true){
		clint.SendDataAsync("sdfasdfsfsdfs\r\n",25);
	}
}

